package cn.com.blueInfo.bpm.common.controller;

import cn.com.blueInfo.bpm.common.response.EnumVO;
import cn.com.blueInfo.utils.result.ResultInfo;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Tag(name = "获取所有固定枚举值的控制器")
@RestController
@RequestMapping("/enums")
public class EnumController {

    private static final String RESOURCE_PATTERN = "/**/*.class";

    /**
     * 获取所有固定枚举类信息
     * @return ResultInfo
     */
    @GetMapping("/getInfos")
    public ResultInfo<List<EnumVO>> getAllEnums() {
        List<Class<? extends Enum>> enumClasses = scan("cn.com.blueInfo.bpm");
        List<EnumVO> enumVOList = enumClasses.stream()
                .map(EnumVO::new).collect(Collectors.toList());
        return ResultInfo.<List<EnumVO>>builder().data(enumVOList).build();
    }

    /**
     * 根据枚举类名获取特定枚举信息
     */
    @GetMapping("/getInfo/{enumSimpleName}")
    public ResultInfo<EnumVO> getEnumBySimpleName(@PathVariable String enumSimpleName) {
        List<Class<? extends Enum>> enumClasses = scan("cn.com.blueInfo.bpm");
        EnumVO enumVO = enumClasses.stream()
                .filter(clazz -> clazz.getSimpleName().equals(enumSimpleName))
                .map(EnumVO::new)
                .findFirst()
                .orElse(null);
        return ResultInfo.<EnumVO>builder().data(enumVO).build();
    }

    /**
     * 扫描指定包下的所有枚举类
     * @param basePackage 基础包路径
     * @return 枚举类列表
     */
    private List<Class<? extends Enum>> scan(String basePackage) {
        List<Class<? extends Enum>> enums = new ArrayList<>();
        try {
            ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
            String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +
                    ClassUtils.convertClassNameToResourcePath(basePackage) + RESOURCE_PATTERN;
            Resource[] resources = resolver.getResources(pattern);

            MetadataReaderFactory readerFactory = new CachingMetadataReaderFactory(resolver);
            for (Resource resource : resources) {
                if (resource.isReadable()) {
                    MetadataReader reader = readerFactory.getMetadataReader(resource);
                    String className = reader.getClassMetadata().getClassName();
                    Class<?> clazz = Class.forName(className);

                    // 检查是否为枚举类
                    if (clazz.isEnum()) {
                        enums.add((Class<? extends Enum>) clazz);
                    }
                }
            }
        } catch (Exception e) {
            throw new RuntimeException("扫描枚举类失败", e);
        }
        return enums;
    }

}
